#pragma once

#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t *lock_p = nullptr) : _plock(lock_p) {}

    void lock()
    {
        if (_plock)
        {
            pthread_mutex_lock(_plock);
        }
    }
    void unlock()
    {
        if (_plock)
        {
            pthread_mutex_unlock(_plock);
        }
    }

private:
    pthread_mutex_t *_plock;
};

class LockGuard
{
    LockGuard(const LockGuard &) = delete;
    LockGuard& operator=(const LockGuard &) = delete;
public:
    /// @brief 作用域自动上锁解锁
    /// @param mutex 锁的地址
    LockGuard(pthread_mutex_t * mutex) : _mutex(mutex)
    {
        _mutex.lock();
    }
    ~LockGuard()
    {
        _mutex.unlock();
    }

private:
    Mutex _mutex;
};